﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using SMSclass;
namespace LoginFormNew
{
    public partial class frmSMS : Form
    {
        public frmSMS()
        {
         //   InitializeComponent();
        }
        public static SerialPort _PORT = new SerialPort();
        private void frmSMS_Load(object sender, EventArgs e)
        {

        }
        public static bool AutoConnect()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                if (TryConnect(port))
                {
                    return true;
                }
            }
            return false;
        }
        // public static AutoResetEvent receiveNow;
        public static bool TryConnect(string portName)
        {
            try
            {
                _PORT = clsSMS.OpenPort(portName, 9600, 8, 300, 300);
                if (_PORT != null)
                {
                    clsSMS.ExecATCommand(_PORT, "AT", 300, "Không có modem nào được kết nối!");
                    return true;
                }
                else
                {
                    clsSMS.ClosePort(_PORT);
                    //ClosePort();
                    return false;
                }
            }
            catch
            {
                clsSMS.ClosePort(_PORT);
                //ClosePort();
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (AutoConnect())
            {
                if (clsSMS.sendMsg(_PORT, "1234", "000"))
                    MessageBox.Show("SMS delivered successfully!", "MESSAGE", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show("Could not send message", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
       
    }
}
